var gulp = require('gulp'), 
//  server = require('gulp-express'),
//		browserSync = require('browser-sync').create(),
	inject = require('gulp-inject'),
    livereload = require('gulp-livereload')
    ;
	/**
	 * 自动刷新网页任务，同时启动服务器
	 */
    gulp.task('autoReload', function() {
      server = require('../server/server.js');
      livereload.listen();
      gulp.watch(['src/**']).on('change', livereload.reload);
    });
    /**
     * js ，css文件添加或删除后重新注入主页
     */
	gulp.task('watchFiles',function(){
		gulp.watch(['./src/js/**','./src/css/**','./src/plugin/**'])
		    .on('added',inject)
		    .on('deleted',inject);
	});
	/***
	 * 将css ，js 自动注入到主页里
	 */
	gulp.task('inject',function(){
		var controller = gulp.src(['./src/js/controller/*.js'],{read: false});
		var directive = gulp.src(['./src/js/directive/*.js'],{read: false});
		var filter = gulp.src(['./src/js/filter/*.js'],{read: false});
		var server = gulp.src(['./src/js/server/*.js'],{read: false});
		
		var router = gulp.src(['./src/js/router-*.js'],{read: false});
		var config = gulp.src(['./src/js/config.js'],{read: false});
		var plugin = gulp.src([
			'./src/plugin/*.min.js',
			'./src/plugin/*.css',
			'./src/plugin/bootstrap/bootstrap.min.js',
			'./src/plugin/bootstrap/bootstrap.min.css'
		],{read: false});
		
		gulp.src('./src/index.html')
			.pipe(inject(controller , {name: 'controller',relative:true}))
			.pipe(inject(directive , {name: 'directive',relative:true}))
			.pipe(inject(filter , {name: 'filter',relative:true}))
			.pipe(inject(server , {name: 'server',relative:true}))
			.pipe(inject(router , {name: 'router',relative:true}))
			.pipe(inject(config , {name: 'config',relative:true}))
			.pipe(inject(plugin , {name: 'plugin',relative:true}))
			
			.pipe(gulp.dest('./src') );
//			.pipe(inject(directive , {starttag:'<!-- inject:directive:{{ext}} -->'}))
//			.pipe(inject(filter , {starttag:'<!-- inject:filter:{{ext}} -->'}))
//			.pipe(inject(aServer , {starttag:'<!-- inject:server:{{ext}} -->'}))
		
	});
	
	
	
	/*
	var sourcesIndex = gulp.src(['./srcindex.js', './srcindex.css'], {read: false});
	var sourcesCommon = gulp.src(['./srccommon.js', './srccommon.css'], {read: false});
	gulp.src('./src/index.html')
	    .pipe(inject(sourcesIndex, {starttag: '<!-- inject:index:{{ext}} -->'}))
	    .pipe(inject(sourcesCommon, {starttag: '<!-- inject:common:{{ext}} -->'}))
	    .pipe(gulp.dest('./dist'));
    */